package org.apaydin;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import org.apaydin.beyazperde.BeyazperdeWeeklyFilms;
import org.apaydin.beyazperde.Film;
import org.apaydin.beyazperde.Week;
import org.apaydin.widget.BitmapUtils;
import org.apaydin.widget.SpinnerArrayAdapter;
import org.apaydin.widget.StringUtils;
import org.apaydin.widget.ToString;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class BeyazActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Week week = new Week(Calendar.getInstance());
        List<Week> weeks = new ArrayList<Week>();
        for (int i = -5; i <= 5; i++) {
            weeks.add(week.addWeek(i));
        }

        final Spinner weekSpinner = (Spinner) findViewById(R.id.weekSpinner);
        SpinnerArrayAdapter<Week> adapter = new SpinnerArrayAdapter<Week>(this, R.layout.my_spinner, android.R.layout.simple_spinner_dropdown_item, weeks, new ToString<Week>() {
            public String toString(Week value) {
                return value.getWeekString();
            }
        }, new ToString<Week>() {
            public String toString(Week value) {
                return value.toString();
            }
        }
        );
        weekSpinner.setAdapter(adapter);


        findViewById(R.id.btnRetrieveFilms).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                setNextOrPreWeek(weekSpinner, 0);
            }
        });

        findViewById(R.id.btnNextWeek).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                setNextOrPreWeek(weekSpinner, 1);
            }
        });

        findViewById(R.id.btnPreviousWeek).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                setNextOrPreWeek(weekSpinner, -1);
            }
        });


    }

    private void setNextOrPreWeek(Spinner weekSpinner, int add){
        int selectedItemPosition = weekSpinner.getSelectedItemPosition();
        int pos = selectedItemPosition + add;
        if(pos < 0 || pos >= weekSpinner.getCount()){
            return;
        }
        weekSpinner.setSelection(pos, true);
        Week week = (Week) weekSpinner.getSelectedItem();
        onWeekSelected(week);
    }

    private void onWeekSelected(Week week){

        try {
            BeyazperdeWeeklyFilms films = new BeyazperdeWeeklyFilms(week);
            films.fetch();
            renderFilmList(films.getFilms());
//            List<Film> list = new ArrayList<Film>();
//            list.add(new Film("Yenilmezler" + new Date(), "http://tri.acimg.net/cx_160_213/b_1_d6d6d6/medias/nmedia/18/85/49/05/20085945.jpg"));
//            list.add(new Film("Yenilmezler" + new Date(), "http://tri.acimg.net/cx_160_213/b_1_d6d6d6/medias/nmedia/18/85/49/05/20085945.jpg"));
//            list.add(new Film("Yenilmezler" + new Date(), "http://tri.acimg.net/cx_160_213/b_1_d6d6d6/medias/nmedia/18/85/49/05/20085945.jpg"));
//            list.add(new Film("Yenilmezler" + new Date(), "http://tri.acimg.net/cx_160_213/b_1_d6d6d6/medias/nmedia/18/85/49/05/20085945.jpg"));
//            list.add(new Film("Yenilmezler" + new Date(), "http://tri.acimg.net/cx_160_213/b_1_d6d6d6/medias/nmedia/18/85/49/05/20085945.jpg"));
//            list.add(new Film("Yenilmezler" + new Date(), "http://tri.acimg.net/cx_160_213/b_1_d6d6d6/medias/nmedia/18/85/49/05/20085945.jpg"));
//            list.add(new Film("Yenilmezler" + new Date(), "http://tri.acimg.net/cx_160_213/b_1_d6d6d6/medias/nmedia/18/85/49/05/20085945.jpg"));
//            renderFilmList(list);
        } catch (Exception ex) {
            Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_SHORT);
        }
    }

    private void renderFilmList(List<Film> films) {
        ListView panel = (ListView) findViewById(R.id.listViewFilms);
        panel.setAdapter(new FilmListAdaptor(R.layout.film_info, films));
    }


    private class FilmListAdaptor extends ArrayAdapter<Film> {

        private final List<Film> films;

        class ViewHolder {
            public TextView turkishName;
            public TextView stars;
            public TextView director;
            public TextView releaseDate;
            public ImageView posterImage;
        }

        public FilmListAdaptor(int resource, List<Film> films) {
            super(getApplicationContext(), resource, films);
            this.films = films;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View rowView = convertView;
            if (rowView == null) {
                LayoutInflater inflater = getLayoutInflater();
                rowView = inflater.inflate(R.layout.film_info, null);
                ViewHolder viewHolder = new ViewHolder();
                viewHolder.turkishName = (TextView) rowView.findViewById(R.id.film_info_turkish_name);
                viewHolder.director = (TextView) rowView.findViewById(R.id.film_info_director);
                viewHolder.releaseDate = (TextView) rowView.findViewById(R.id.film_info_release_date);
                viewHolder.stars = (TextView) rowView.findViewById(R.id.film_info_stars);
                viewHolder.posterImage = (ImageView) rowView.findViewById(R.id.film_info_poster_image);
                rowView.setTag(viewHolder);
            }

            ViewHolder holder = (ViewHolder) rowView.getTag();
            Film film = films.get(position);
            holder.turkishName.setText(film.getTurkishName());
            holder.director.setText(film.getDirector());
            holder.releaseDate.setText(StringUtils.toString(film.getReleaseDate()));
            holder.stars.setText(StringUtils.toString(film.getStars(), " ,"));
            holder.posterImage.setImageBitmap(film.getPosterThumbBitmap());

            return rowView;
        }

    }


}
